/**
 * 
 */
package edi.android.tmdbBrowser;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Locale;

import net.sf.jtmdb.Movie;
import net.sf.jtmdb.Country;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RatingBar;
import android.widget.TextView;

/**
 * @author edi
 * 
 */
public class ShowMovieActivity extends Activity {
	private int movieID;
	private ShowMovieAdapter adapter;
	@SuppressWarnings("unused")
	private static final SimpleDateFormat sdfYear = new SimpleDateFormat("yyyy");
	@SuppressWarnings("unused")
	private static final DateFormat dfLocal = DateFormat.getDateInstance();

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// Set up view and start progress bar
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
		setContentView(R.layout.movie);
		adapter = new ShowMovieAdapter();
		((ListView) findViewById(R.id.movieListview)).setAdapter(adapter);
		setProgressBarIndeterminateVisibility(true);

		// get the movie id and then the movie
		Bundle extras = getIntent().getExtras();
		movieID = extras.getInt("movieID");
		setFields(movieID);

		// stop the progress bar
		setProgressBarIndeterminateVisibility(false);
	}

	/**
	 * @param movieID
	 *            TMDb movie ID of the movie to show
	 */
	private void setFields(int movieID) {
		// TODO: replace by get movie by id... at the moment, we get it by list
		// position from most popular
		final Movie movie = MovieDownloader.get(movieID);
		LayoutInflater layoutInflater = (LayoutInflater) this
				.getSystemService(LAYOUT_INFLATER_SERVICE);
		View view;
		String tempString;

		/*
		 * Fill the top field
		 */
		// get the view
		view = layoutInflater.inflate(R.layout.movie_topfield, null);

		// set the name
		((TextView) view.findViewById(R.id.movie_topfieldName)).setText(movie
				.getName());

		// set the poster, if any
		if (movie.getImages().posters != null
				&& movie.getImages().posters.iterator().hasNext()) {
			tempString = movie.getImages().posters.iterator().next()
					.getSmallestImage().toString();
			TMDbApp.imageDownloader.download(tempString, (ImageView) view
					.findViewById(R.id.movie_topfieldPoster));
		} else {
			((ImageView) view.findViewById(R.id.movie_topfieldPoster))
					.setVisibility(View.GONE);
		}
		// set the tagline, if any
		tempString = movie.getTagline();
		if (tempString != null && !tempString.equalsIgnoreCase("null")
				&& tempString.length() > 2) {
			((TextView) view.findViewById(R.id.movie_topfieldTagline))
					.setText("\"" + tempString + "\"");
		} else {
			view.findViewById(R.id.movie_topfieldTagline).setVisibility(
					View.GONE);
		}

		// set the original name, if any and different from movie name
		tempString = movie.getOriginalName();
		if (tempString != null && !tempString.equalsIgnoreCase("null")
				&& !tempString.equalsIgnoreCase(movie.getName())) {
			((TextView) view.findViewById(R.id.movie_topfieldOriginalName))
					.setText(getString(R.string.prefixOriginal) + " "
							+ tempString);
		} else {
			view.findViewById(R.id.movie_topfieldOriginalName).setVisibility(
					View.GONE);
		}

		// set the alternative name, if any and different from movie name or
		// original name
		tempString = movie.getAlternativeName();
		if (tempString != null && !tempString.equalsIgnoreCase("null")
				&& !tempString.equalsIgnoreCase(movie.getName())
				&& !tempString.equalsIgnoreCase(movie.getOriginalName())) {
			((TextView) view.findViewById(R.id.movie_topfieldAlternativeName))
					.setText(getString(R.string.prefixAlternative) + " "
							+ tempString);
		} else {
			view.findViewById(R.id.movie_topfieldAlternativeName)
					.setVisibility(View.GONE);
		}

		// set certification, if any
		tempString = movie.getCertification();
		if (tempString != null && !tempString.equalsIgnoreCase("null")) {
			((TextView) view.findViewById(R.id.movie_topfieldCertification))
					.setText(getString(R.string.movieCertification)
							+ tempString);
		} else {
			view.findViewById(R.id.movie_topfieldCertification).setVisibility(
					View.GONE);
		}

		// add the view to the list
		adapter.addItem(new ShowMovieItem(view, false, ShowMovieItem.TOPINFO));

		/*
		 * Put overview if there is any
		 */
		tempString = movie.getOverview();
		if (tempString != null && !tempString.equalsIgnoreCase("null")
				&& !tempString.equalsIgnoreCase("no overview found.")
				&& tempString.length() > 2) {
			// get a header view
			view = layoutInflater.inflate(R.layout.movie_header, null);

			// set the header
			((TextView) view.findViewById(R.id.movie_headerText))
					.setText(R.string.movieOverviewTitle);

			// add it to the list
			adapter.addItem(new ShowMovieItem(view, false,
					ShowMovieItem.SEPARATOR));

			// get a plain text view
			view = layoutInflater.inflate(R.layout.movie_plain, null);

			// set the text
			((TextView) view.findViewById(R.id.movie_plainText))
					.setText(tempString);

			// add it to the list
			adapter
					.addItem(new ShowMovieItem(view, false, ShowMovieItem.PLAIN));
		}

		/*
		 * Put rating
		 */
		// get a header view
		view = layoutInflater.inflate(R.layout.movie_header, null);

		// set the header
		((TextView) view.findViewById(R.id.movie_headerText))
				.setText(R.string.movieRatingTitle);
		adapter
				.addItem(new ShowMovieItem(view, false, ShowMovieItem.SEPARATOR));

		// fill the fields in the string
		tempString = getString(R.string.movieRating);
		tempString = tempString.replace("%RATING%", "" + movie.getRating());
		tempString = tempString.replace("%VOTES%", "" + movie.getVotes());

		// get a rating view
		view = layoutInflater.inflate(R.layout.movie_rating, null);

		// set values
		((RatingBar) view.findViewById(R.id.movie_ratingBar))
				.setRating((float) movie.getRating());
		((TextView) view.findViewById(R.id.movie_ratingVotes))
				.setText(tempString);

		adapter.addItem(new ShowMovieItem(view, false, ShowMovieItem.RATING));

		/*
		 * Production Information
		 */

		// check if we got any produciton information
		if (movie.getLanguage() != null
				&& !movie.getLanguage().equalsIgnoreCase("null")
				&& movie.getCountries().size() > 0) {

			// we need a locale to have localized country and language names
			Locale locale;

			// get a header view
			view = layoutInflater.inflate(R.layout.movie_header, null);

			// set the header
			((TextView) view.findViewById(R.id.movie_headerText))
					.setText(R.string.movieProductionInfoTitle);
			adapter.addItem(new ShowMovieItem(view, false,
					ShowMovieItem.SEPARATOR));

			// if we have any country, print it
			if (movie.getCountries().size() > 0) {
				// get the view
				view = layoutInflater.inflate(R.layout.movie_twoline, null);

				// have we got more than one country?
				if (movie.getCountries().size() == 1) {
					((TextView) view.findViewById(R.id.movie_twolineTitle))
							.setText(R.string.movieCountryTitle);
				} else {
					((TextView) view.findViewById(R.id.movie_twolineTitle))
							.setText(R.string.movieCountriesTitle);
				}

				tempString = "";
				for (Country country : movie.getCountries()) {
					locale = new Locale(Locale.getDefault().getLanguage(),
							country.getCode());
					tempString += locale.getDisplayCountry() + ", ";
				}
				// cut off the last coma and space when setting the countries
				((TextView) view.findViewById(R.id.movie_twolineText))
						.setText(tempString.subSequence(0,
								tempString.length() - 2));

				adapter.addItem(new ShowMovieItem(view, false,
						ShowMovieItem.TWOLINE));
			}

			// TODO: implement multiple spoken languages as soon as they are
			// available in the API
			// If we have any spoken language, print it

			if (movie.getLanguage() != null
					&& !movie.getLanguage().equalsIgnoreCase("null")) {
				// inflate a view
				view = layoutInflater.inflate(R.layout.movie_twoline, null);

				// set title, at the moment in singular, as there is only one
				// language returned from the api
				((TextView) view.findViewById(R.id.movie_twolineTitle))
						.setText(R.string.movieLanguageTitle);
				locale = new Locale(movie.getLanguage());
				((TextView) view.findViewById(R.id.movie_twolineText))
						.setText(locale.getDisplayLanguage());

				// add the view to the list
				adapter.addItem(new ShowMovieItem(view, false,
						ShowMovieItem.TWOLINE));

			}

		}
		// {
		// Locale locale = new Locale(movie.getLanguage());
		// movieLanguage.setText(locale.getDisplayLanguage());
		// } else {
		// movieLanguage.setVisibility(View.GONE);
		// findViewById(R.id.movieLanguageTitle).setVisibility(View.GONE);
		// }
		// // collect fields for primary information
		// TextView movieName = (TextView) findViewById(R.id.movieName);
		// ImageView moviePoster = (ImageView) findViewById(R.id.moviePoster);
		// RatingBar movieRating = (RatingBar) findViewById(R.id.movieRating);
		// TextView movieAlternativeName = (TextView)
		// findViewById(R.id.movieAlternativeName);
		// TextView movieRelease = (TextView) findViewById(R.id.movieRelease);
		// TextView movieOverview = (TextView) findViewById(R.id.movieOverview);
		// TextView movieCountry = (TextView) findViewById(R.id.movieCountry);
		// TextView movieLanguage = (TextView) findViewById(R.id.movieLanguage);
		// Button movieTrailer = (Button) findViewById(R.id.movieTrailer);
		//
		// movieName.setText(movie.getName());
		//
		// if (movie.getImages().posters != null
		// && movie.getImages().posters.iterator().hasNext()) {
		// String url = movie.getImages().posters.iterator().next()
		// .getSmallestImage().toString();
		// Log.d("ShowMovieActivity", url);
		// TMDbApp.imageDownloader.download(url, moviePoster);
		// } else {
		// moviePoster.setVisibility(View.GONE);
		// }
		//
		// movieRating.setRating((float) movie.getRating());
		//		
		// TMDbApp app = (TMDbApp) getApplication();
		// String secondary = app.chooseSecondaryName(movie, true);
		//		
		// if (secondary.equals("null")) {
		// movieAlternativeName.setVisibility(View.GONE);
		// } else {
		// movieAlternativeName.setText(secondary);
		// }
		//
		//
		// if (movie.getReleasedDate() != null) {
		// movieRelease.setText(getString(R.string.movieRelease)
		// + " "
		// + android.text.format.DateFormat.getDateFormat(
		// getApplicationContext()).format(
		// movie.getReleasedDate()));
		// this.setTitle(movie.getName() + " ("
		// + TMDbApp.sdfYear.format(movie.getReleasedDate()) + ")");
		// } else {
		// movieRelease.setVisibility(View.GONE);
		// this.setTitle(movie.getName());
		// }
		//
		// if (movie.getLanguage() != null && movie.getLanguage().length() > 0)
		// {
		// Locale locale = new Locale(movie.getLanguage());
		// movieLanguage.setText(locale.getDisplayLanguage());
		// } else {
		// movieLanguage.setVisibility(View.GONE);
		// findViewById(R.id.movieLanguageTitle).setVisibility(View.GONE);
		// }
		//
		// if (movie.getCountries() != null
		// && movie.getCountries().iterator().hasNext()) {
		// String countries = new String();
		// Locale locale;
		//
		// for (Country country : movie.getCountries()) {
		// locale = new Locale(Locale.getDefault().getLanguage(), country
		// .getCode());
		// countries += locale.getDisplayCountry() + ", ";
		// Log.d("ShowMovieActivity", country.getCode() + "->"
		// + locale.getDisplayCountry());
		//
		// }
		// // cut off the last coma and space when setting the countries
		// movieCountry.setText(countries.subSequence(0,
		// countries.length() - 2));
		// } else {
		// movieCountry.setVisibility(View.GONE);
		// findViewById(R.id.movieCountryTitle).setVisibility(View.GONE);
		// }
		//
		// if (movie.getOverview() != null &&
		// !movie.getOverview().equals("null")) {
		// movieOverview.setText(movie.getOverview());
		// } else {
		// movieOverview.setVisibility(View.GONE);
		// findViewById(R.id.movieOverviewTitle).setVisibility(View.GONE);
		// }
		//
		// if (movie.getTrailer() != null) {
		// movieTrailer.setOnClickListener(new OnClickListener() {
		// @Override
		// public void onClick(View view) {
		// final String url = movie.getTrailer().toString();
		// startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
		// }
		// });
		// } else {
		// movieTrailer.setVisibility(View.GONE);
		// findViewById(R.id.movieTrailerTitle).setVisibility(View.GONE);
		// }
	}
}
